package com.gzz.orchid.common.webmvc;

import com.gzz.orchid.common.webmvc.support.GlobalExceptionHandler;
import com.gzz.orchid.common.webmvc.support.limiter.LimiterConfiguration;
import com.gzz.orchid.common.webmvc.support.repeatsubmit.ReSubmitAspect;
import com.gzz.orchid.common.webmvc.support.vistlog.VisitLogAspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author higmd
 */
@Configuration
public class GlobalConfiguration {
    static {
        System.out.println("***启动WebMvc时加载全局配置***");
    }
    /**
     * 启动时打印参数
     *
     * @return
     */
    @Bean
    public SpringApplicationRunner springApplicationRunner() {
        System.out.println("***启用启动时应用预处理***");
        return new SpringApplicationRunner();
    }
    @Bean
    public SpringCommandLineRunner springCommandLineRunner() {
        System.out.println("***启用启动时命令行预处理***");
        return new SpringCommandLineRunner();
    }
    /**
     * 全局异常处理
     */
    @Bean
    @ConditionalOnProperty(prefix = "feature.global",name = "exception", havingValue = "true")
    public GlobalExceptionHandler globalExceptionHandler() {
        System.out.println("***启用全局异常处理***");
        return new GlobalExceptionHandler();
    }

    /**
     * 访问日志
     */
    @Bean
    @ConditionalOnProperty(prefix = "feature.global",name = "visitlog", havingValue = "true")
    public VisitLogAspect visitLogAspect() {
        System.out.println("***启用访问日志***");
        return new VisitLogAspect();
    }

    /**
     * 限流
     */
    @Bean
    @ConditionalOnProperty(prefix = "feature.global",name = "limiter", havingValue = "true")
    public LimiterConfiguration limiterConfiguration() {
        System.out.println("***启用限流***");
        return new LimiterConfiguration();
    }

    /**
     * 防重复提交
     */
    @Bean
    @ConditionalOnBean(name = "redisTemplate")
    @ConditionalOnProperty(prefix = "feature.global",name = "security", havingValue = "true")
    public ReSubmitAspect reSumitAspect() {
        System.out.println("***启用防重复提交***");
        return new ReSubmitAspect();
    }

}
